package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.CustomerManager;
import service.impl.CustomerManagerFactory;
import verify.MyVerify;

/**
 * Servlet implementation class DeleteCustomerServlet
 */
@WebServlet("/delete")
public class DeleteCustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MyVerify myVerify;
	private CustomerManager manager;
       
    public DeleteCustomerServlet() {
        super();
       myVerify = new MyVerify();
       manager = CustomerManagerFactory.getInstance();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cid = request.getParameter("id");
		String verify = myVerify.verifyInteger(cid);
		if (!verify.equals(MyVerify.SUCCESS)) {
			response.sendRedirect("error?info="+verify);
			return;
		}
		Integer id = Integer.parseInt(cid);
		int m = manager.deleteCustomer(id);
		System.out.println(m);
		if (m>0) {
			response.sendRedirect("list");
		}else{
			response.sendRedirect("error?info=delete fail");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
